package hotel;


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;

import java.util.ArrayList;
import java.util.Calendar;

public class userApplication implements ActionListener {
	protected static JFrame testframe; // static so the class progressBar can
	// dispose of it.
	private JPanel panel;
	private JFrame frame;
	private JButton checkinButton, checkoutButton;
	private String userName;
	private User user;
	private JPanel masterPanel;
	private JTextArea inTxtArea;
	private JTextArea outTxtArea;
	protected  Integer checkinDay;
	protected  Integer checkinMonth;
	protected  Integer checkinYear;
	protected  Integer checkoutDay;
	protected  Integer checkoutMonth;
	protected  Integer checkoutYear;
	JTextArea outTxtArea2;


	/**
	 * This constructor creates a login window, and also adds components to the
	 * content pane.
	 */
	public userApplication(User user1) 
	{
		user = user1;


		panel = new JPanel(new BorderLayout());
		try {
			UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}

		frame = new JFrame("Hotel Booking : " + user.getName());
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		frame.setSize(new Dimension(700, 215));
		frame.setResizable(false);
		frame.setLocation((int) (screenSize.width * 0.25), (int) (screenSize.height * 0.25));
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLayout(new BorderLayout());


		checkinYear = 2011;
		checkinMonth = 5;
		checkinDay =  1;
		checkoutYear = 2011;
		checkoutMonth = 5;
		checkoutDay =  1;

		createHotelAndRoomNumField();
		createButtons();

		frame.add(panel, BorderLayout.CENTER);
		frame.setVisible(true);
	}

	/**
	 * Void method that creates the ID and Password Fields and adds them to the
	 * panel.
	 */

	private void updateText(){
	
		
		if(user.getBookings().size() == 0 ){
			
		}
		else{
					outTxtArea2.append("" + user.getBookings().get(  user.getBookings().size()-1  ).toString() + "\n");
			}
		}
		
	
	private void createHotelAndRoomNumField() 
	{
		masterPanel = new JPanel(new FlowLayout());

		outTxtArea = new JTextArea("Check-in : " +  checkinMonth + "-"+ checkinDay + "-"+ checkinYear );
		outTxtArea.setEditable(false);
		outTxtArea.setHighlighter(null);
		masterPanel.add(outTxtArea);
		//masterPanel.setBackground(Color.white);
		inTxtArea = new JTextArea("Check-out :" + checkoutMonth + "-"+ checkoutDay + "-"+ checkoutYear);
		inTxtArea.setEditable(false);
		inTxtArea.setHighlighter(null);
		masterPanel.add(inTxtArea);
		masterPanel.add(new JSeparator());
		panel.add(masterPanel, BorderLayout.NORTH);
		
		
		JPanel masterPanel2 = new JPanel(new FlowLayout());
		outTxtArea2 = new JTextArea();
		outTxtArea2.append("Your Bookings: ");
		
		outTxtArea2.setEditable(false);
		outTxtArea2.setHighlighter(null);
		
		updateText();
		masterPanel2.add(outTxtArea2);
		JScrollPane spane = new JScrollPane(masterPanel2);
		panel.add(spane, BorderLayout.SOUTH);


		//panel.setBackground(Color.white);
		JMenuBar menuBar = new JMenuBar();
		JMenu monthMenu = new JMenu();
		menuBar.add(monthMenu);

	}

	/**
	 * Void methods that creates the buttons and adds them to the panel.
	 */
	public void createButtons() {
		JPanel bPanel = new JPanel(new BorderLayout());
		JPanel buttonPanel = new JPanel(new FlowLayout());
		JPanel buttonPanel2 = new JPanel(new FlowLayout());
		JPanel buttonPanel3 = new JPanel(new FlowLayout());
		checkinButton = new JButton(" Change Check-in Date ");
		checkinButton.setActionCommand("checkIn");
		checkinButton.addActionListener(this);
		checkoutButton = new JButton("Change Check-out Date");
		checkoutButton.setActionCommand("checkOut");
		checkoutButton.addActionListener(this);
		JButton logoutButton = new JButton();
		logoutButton = new JButton("    Logout   ");
		logoutButton.setActionCommand("Logout");
		logoutButton.addActionListener(this);
		JButton findHotelsButton = new JButton("Book a Hotel!");
		findHotelsButton.setActionCommand("FindHotels");
		findHotelsButton.addActionListener(this);
		buttonPanel.add(checkinButton);
		buttonPanel.add(new JSeparator());
		buttonPanel.add(checkoutButton);
		buttonPanel2.add(findHotelsButton);
		buttonPanel3.add(logoutButton);	
		bPanel.add(buttonPanel, BorderLayout.NORTH);
		bPanel.add(buttonPanel2, BorderLayout.CENTER);
		bPanel.add(buttonPanel3, BorderLayout.SOUTH);
		panel.add(bPanel, BorderLayout.CENTER);
		
		//cp.setBackground(Color.white);
	}

	/**
	 * Action Performed method that overrides actionListener's actionPerformed
	 * method.
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		String c = e.getActionCommand();
		if (c.equals("checkIn")) // Add new hotel
		{
			new dateChooser(c, this);
		}
		else if (c.equals("checkOut")) // remove hotel
		{
			new dateChooser(c, this);
		} 
		else if (c.equals("Logout")) // logout
		{
			new loginWindow(loginWindow.database);
			frame.dispose();
		}
		else if (c.equals("FindHotels")) // logout
		{

			ArrayList<Hotel> hotels = loginWindow.database.getHotelList();
			Object[] possibilities = new Object[hotels.size()];

			for(int i = 0; i < hotels.size(); i++){
				possibilities[i] = hotels.get(i).getName();
			}
			String s = (String)JOptionPane.showInputDialog(
					frame,
					"Choose A Hotel",
					"Hotels",
					JOptionPane.PLAIN_MESSAGE,
					new ImageIcon(),
					possibilities,
					"idk");

			bookHotel(s);
			

		}
	}
	private void bookHotel(String name){

		ArrayList<Hotel> hotels = loginWindow.database.getHotelList();
		Hotel hotelToBook = hotels.get(0);
		for(int i = 0; i<hotels.size(); i++){
			if(hotels.get(i).getName().equals(name)){
				hotelToBook = hotels.get(i);
			}
		}
		user.addBooking(hotelToBook, checkinDay, checkoutDay);
		
		if(user.getBookings().get( user.getBookings().size()-1  ).isLegit()  ){
			JOptionPane.showMessageDialog(frame,
					"Hotel Booked!",
					"Success!",
					JOptionPane.PLAIN_MESSAGE);
			updateText();
		}
		else{
			JOptionPane.showMessageDialog(frame,
					"Hotel Not Booked! No more rooms!",
					"Fail!",
					JOptionPane.PLAIN_MESSAGE);
		}
	}
	protected void update(int year, int month, int day, String type)
	{
		if(type.equals("checkIn"))
		{
			checkinYear = year;
			checkinMonth = month;
			checkinDay = day;
		}
		else
		{
			checkoutYear = year;
			checkoutMonth = month;
			checkoutDay = day;
		}

		if(checkinYear > checkoutYear)
		{
			checkoutYear  = checkinYear;
			checkoutMonth =  checkinMonth;
			checkoutDay =  checkinDay;
		} 

		else if(checkinMonth > checkoutMonth)
		{
			checkoutMonth =  checkinMonth;
			checkoutDay =  checkinDay;
		} else if((checkinMonth - checkoutMonth) == 0 && checkinDay > checkoutDay)
		{
			checkoutDay = checkinDay;
		}
		System.out.println("Check-in : " +  checkinMonth + "-"+ checkinDay + "-"+ checkinYear);
		System.out.println("Check-out :" + checkoutMonth + "-"+ checkoutDay + "-"+ checkoutYear);
		masterPanel.removeAll();
		outTxtArea = new JTextArea("Check-in : " +  checkinMonth + "-"+ checkinDay + "-"+ checkinYear );
		inTxtArea = new JTextArea("Check-out :" + checkoutMonth + "-"+ checkoutDay + "-"+ checkoutYear);
		masterPanel.add(outTxtArea);
		masterPanel.add(inTxtArea);
		outTxtArea.repaint();
		inTxtArea.repaint();
		masterPanel.revalidate();
		masterPanel.repaint();

		//frame.repaint();
	}

}
